package at.fhj.pse.pageobjects;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;

public class IntervalsPerDayPage{
    private WebDriver driver;
    
    /*
     * Constructor
     */
    public IntervalsPerDayPage(WebDriver driver){
        this.driver = driver;
    }
    

    /*
     * Input field: username:String
     */
    private String username;
    public String getUsername(){
        return username;
    }
    public void setUsername(String username){
        this.username = username;
    }
    
    
    /*
     * Input field: password:String
     */
    private String password;
    public String getPassword(){
        return password;
    }
    public void setPassword(String password){
        this.password = password;
    }
    
    /*
     * Input field: acceptNextAlert:Boolean
     */
    private Boolean acceptNextAlert;
    public Boolean getacceptNextAlert(){
    	return acceptNextAlert;
    }
    public void setacceptNextAlert(Boolean acceptNextAlert){
    	this.acceptNextAlert = acceptNextAlert;
    }
    
    /*
	 * Input field: date1:Date
	 */
	private String date;
	public String getDate() {
		
		return date;
	}
	public void setDate(String string) {
		
		this.date = string;
	}
    
    /*
     * Action: ShowIntervalsPerDay
     */
    public String showIntervalsPerDay() throws InterruptedException{
		driver.findElement(By.id("j_idt35:intervalsByDay_input")).click();
		driver.findElement(By.id("j_idt35:intervalsByDay_input")).clear();
		driver.findElement(By.id("j_idt35:intervalsByDay_input")).sendKeys(getDate());
		driver.findElement(By.id("j_idt35:intervalsByDay_input")).sendKeys(Keys.RETURN);
		
		
    	String smsg = getSelectorMessage("Forschungsarbeit", "tr.ui-widget-content.ui-datatable-even > td");
		assertEquals("Forschungsarbeit", smsg);

		String xmsg = getPathMessage("08:00", "//tbody[@id='j_idt35:outFactorOne_data']/tr/td[2]");
		assertEquals("08:00", xmsg);

		String xmsg2 = getPathMessage("17:00", "//tbody[@id='j_idt35:outFactorOne_data']/tr/td[3]");
		assertEquals("17:00", xmsg2);

		String xmsg3 = getPathMessage("9h 0min", "//tbody[@id='j_idt35:outFactorOne_data']/tr/td[4]");
		assertEquals("9h 0min", xmsg3);

		String xmsg4 = getPathMessage("Total Amount", "//tfoot[@id='j_idt35:outFactorOnePointFive_foot']/tr[3]/td");
		assertEquals("Total Amount", xmsg4);

		String xmsg5 = getPathMessage("Break", "//tfoot[@id='j_idt35:outFactorOnePointFive_foot']/tr[5]/td");
		assertEquals("Break", xmsg5);

		String xmsg6 = getPathMessage("Actual Time", "//tfoot[@id='j_idt35:outFactorOnePointFive_foot']/tr[6]/td");
		assertEquals("Actual Time", xmsg6);

		String xmsg7 = getPathMessage("Allocated Time", "//tfoot[@id='j_idt35:outFactorOnePointFive_foot']/tr[7]/td");
		assertEquals("Allocated Time", xmsg7);

		String xmsg8 = getPathMessage("Difference", "//tfoot[@id='j_idt35:outFactorOnePointFive_foot']/tr[9]/td");
		assertEquals("Difference", xmsg8);
		
		return driver.findElement(By.xpath("//tbody[@id='j_idt35:outFactorOne_data']/tr/td[4]")).getText();
    }
    
    /*
     * Action: SelectorMessage
     */
    public String getSelectorMessage(String expected, String selector) throws InterruptedException{
    	String cssSelector = driver.findElement(By.cssSelector(selector)).getText();
    	for (int second = 0;; second++) {
        	if (second >= 60) fail("timeout");
        	try { if (expected.equals(cssSelector)) break; } catch (Exception e) {}
        	Thread.sleep(1000);
        }
    	return cssSelector;
    }
    
    /*
     * Action: PathMessage
     */
    public String getPathMessage(String expected, String Path) throws InterruptedException{
    	String xPath = driver.findElement(By.xpath(Path)).getText();
    	for (int second = 0;; second++) {
        	if (second >= 60) fail("timeout");
        	try { if (expected.equals(driver.findElement(By.xpath(Path)).getText())) break; } catch (Exception e) {}
        	Thread.sleep(1000);
        }
    	return xPath;
    }
}
